[PATCH] [mlir][spirv] Account for type conversion failures in scf-to-spirv
authorJakub Kuderski <kubak@google.com>
Mon, 9 Jan 2023 16:35:46 +0000 (11:35 -0500)
committerGianfranco Costamagna <locutusofborg@debian.org>
Mon, 31 Jul 2023 20:16:10 +0000 (21:16 +0100)
Fixes: https://github.com/llvm/llvm-project/issues/59136
Reviewed By: antiagainst

Differential Revision: https://reviews.llvm.org/D141292

Gbp-Pq: Name CVE-2023-29934.patch

mlir/lib/Conversion/SCFToSPIRV/SCFToSPIRV.cpp
mlir/test/Conversion/SCFToSPIRV/if.mlir

index 08e3d3f727627018f84a920f84722b06453f12b3..8f66c1ac98ba0e2c7c92a7802d239758296edec6 100644 (file)
@@ -17,6 +17,7 @@
 #include "mlir/Dialect/SPIRV/Transforms/SPIRVConversion.h"
 #include "mlir/IR/BuiltinOps.h"
 #include "mlir/Transforms/DialectConversion.h"
+#include "llvm/Support/FormatVariadic.h"
 
 using namespace mlir;
 
@@ -274,6 +275,10 @@ IfOpConversion::matchAndRewrite(scf::IfOp ifOp, ArrayRef<Value> operands,
   SmallVector<Type, 8> returnTypes;
   for (auto result : ifOp.results()) {
     auto convertedType = typeConverter.convertType(result.getType());
+    if (!convertedType)
+      return failure();
+
+
     returnTypes.push_back(convertedType);
   }
   replaceSCFOutputValue(ifOp, selectionOp, rewriter, scfToSPIRVContext,
index 40bae1734c252da64f7fc2519656441fb0523da9..921dc479cb8f42306fe9eb5ab83b79e4b943b658 100644 (file)
@@ -153,4 +153,18 @@ func @simple_if_yield_type_change(%arg2 : memref<10xf32>, %arg3 : memref<10xf32>
   return
 }
 
+// Memrefs without a spirv storage class are not supported. The conversion
+// should preserve the `scf.if` and not crash.
+func.func @unsupported_yield_type(%arg0 : memref<8xi32>, %arg1 : memref<8xi32>, %c : i1) {
+// CHECK-LABEL: @unsupported_yield_type
+// CHECK-NEXT:    scf.if
+// CHECK:         spirv.Return
+  %r = scf.if %c -> (memref<8xi32>) {
+    scf.yield %arg0 : memref<8xi32>
+  } else {
+    scf.yield %arg1 : memref<8xi32>
+  }
+  return
+}
+
 } // end module